package com.example.database;

import java.util.ArrayList;
import java.util.List;

import com.exanple.data.Activeinfo;
import com.exanple.data.ChatMsgEntity;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class DbManager {
	private DbHelper helper;  
    private SQLiteDatabase db;  
    public DbManager(Context context) {  
        helper = new DbHelper(context);  
        //因为getWritableDatabase内部调用了mContext.openOrCreateDatabase(mName, 0, mFactory);  
        //所以要确保context已初始化,我们可以把实例化DBManager的步骤放在Activity的onCreate里  
        db = helper.getWritableDatabase();  
    }  
    public void add(ChatMsgEntity enty) {  
        db.beginTransaction();  //开始事务  
        try {  
              
                db.execSQL("INSERT INTO chatmessage (sendname,recname,sendtime,messcontent) VALUES( ?, ?, ?,?)", new Object[]{enty.getSendname(), enty.getRecname(), enty.getSendtime(),enty.getMesscontent()});  
             
            db.setTransactionSuccessful();  //设置事务成功完成  
        } finally {  
            db.endTransaction();    //结束事务  
        }  
    }  
    public List<ChatMsgEntity> query(String username,String username1) {  
        ArrayList<ChatMsgEntity> chatentys = new ArrayList<ChatMsgEntity>();  
        Cursor c = db.rawQuery("SELECT * FROM chatmessage WHERE (sendname= ? AND recname=? ) OR (sendname=? AND recname=? )", new String[]{username,username1,username1,username});
        while (c.moveToNext()) {  
        	ChatMsgEntity chatenty = new ChatMsgEntity(c.getString(c.getColumnIndex("sendname")),c.getString(c.getColumnIndex("recname")),c.getString(c.getColumnIndex("sendtime")),c.getString(c.getColumnIndex("messcontent")));  
            chatentys.add(chatenty);  
        }  
        c.close();  
        return chatentys;  
    }  
    public List<ChatMsgEntity> query(String username) {  
        ArrayList<ChatMsgEntity> chatentys = new ArrayList<ChatMsgEntity>();  
        Cursor c = db.rawQuery("SELECT * FROM chatmessage WHERE sendname= ? OR ecname=? )", new String[]{username});
        while (c.moveToNext()) {  
        	ChatMsgEntity chatenty = new ChatMsgEntity(c.getString(c.getColumnIndex("sendname")),c.getString(c.getColumnIndex("recname")),c.getString(c.getColumnIndex("sendtime")),c.getString(c.getColumnIndex("messcontent")));  
            chatentys.add(chatenty);  
        }  
        c.close();  
        return chatentys;  
    }  
    public void deleteData(){
    	 db.execSQL("delete from chatmessage ");
    }
	public void addactive(Activeinfo active) {
		// TODO Auto-generated method stub
		db.beginTransaction();  //开始事务  
        try {  
              
                db.execSQL("INSERT INTO activeinfo (activename,currentname,leader,parter,location,allip,activetime) VALUES( ?, ?, ?, ?, ?, ?, ?)", new Object[]{active.getActivename(),active.getCurrentname(),active.getLeader(),active.getParter(),active.getLocation(),active.getAllip(),active.getActivetime()});  
             
            db.setTransactionSuccessful();  //设置事务成功完成  
        } finally {  
            db.endTransaction();    //结束事务  
        }  
	}
 
   
}
